<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import { getUrl,confirmModal, ptFloat, minusF } from '@/static/js/common.js';
	import { buildFormData,formValidation, tranferToEditors, changeEditor,setSubmitSubValue, configSelectData, hasValue,formShowLoading ,getFormFields,formModelLoad,getWorkCacheKey} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data(){
			return buildFormData({
				userCacheKey:"hr-report-evaluate-user-cache",
				usersCacheKey:"hr-report-evaluate-users-cache",
				evaUserCacheKey:"hr-report-evaluate-evaUser-cache",
				code:'38542',
				uIds:[]
			})
		},
		computed:{
			...mapState(useLoginUserStore, ['userName','userId'])
		},
		watch:{
			uIds:function(newVal,oldVal){
				if(newVal && newVal.length){
					this.$refs.autoform.formdata.userIds = newVal.map(e => e.id).join(',')
					this.$refs.autoform.formdata.userNames = newVal.map(e => e.username).join(',')
				}
			}
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.checkedit){
					_that.om.formSave('commonForm/doCheckOrApproveUpdate',_that.formdata,_that)
				}else if(_that.edit){
					_that.om.formSave('treportEvaluate/updateData',_that.formdata,_that)
				}else{
					_that.om.formSave('treportEvaluate/addTreportEvaluate',_that.formdata,_that)
				}
			},
			verifyZpos(){
				let _that = this
				if(_that.$refs.autoform.formdata.zposId){
					return true
				}else{
					_that.om.toast('请先选择制表人岗位',3000)
					 return false
				}
			},
			onSelectUser(){
				let _that = this
				_that.om.request(
					'tsysdict/getListByCode',
					 {
						code:_that.code 
					 }
				).then(res=>{
					_that.$refs.autoform.changeEditor('title3','cateCode','data',configSelectData(res,'code','dicname'))
					if(this.edit){
						_that.$refs.autoform.initSelectIndex('title3','cateCode')
					}
				})
			},
			
			del1(data){
				let _that = this
				var newArr = []
				for(let item of data.workList){
					newArr.push(item.interviewElementId)
				}
				_that.lList = newArr
				if(_that.lList.length == 0){
					_that.$refs.autoform.formdata.objStr1 = []
				}
			},
			onSelectZpost(){
				let _that = this
				_that.$refs.autoform.changeEditor('title13', "userIds", {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield:urlUtil.userPageUrl().namefield,
						searchfield:"q",
						initload:true,
						url:urlUtil.userPageUrl().url,
						cachename:_that.usersCacheKey,
						placeholder:"请输入姓名或岗位"
					}
				})
				
			},
			onSelectEndDate(){
				let _that = this
				if(_that.$refs.autoform.formdata.startDate){
					let startTime = new Date(_that.$refs.autoform.formdata.startDate)
					let endTime = new Date(_that.$refs.autoform.formdata.endDate)
					if(endTime < startTime){
						_that.om.toast('截止时间应大于等于开始时间')
						_that.$refs.autoform.clear("title3","endDate")
					}
				}
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					{
						url:"posBusiAuth/isnewList",
						param:{
							code:options.code
						},
						method:'post'
					}
				]
				if(fileId){
					ajaxArr.push({
						url:"treportEvaluate/getMobileInfo",
						param:{
							id:fileId
						}
					})
				}
				let checkedit = (options.checkedit == 'true')
				if(checkedit){
					ajaxArr.push(urlUtil.dictZeroComboboxUrl('36802'))
				}
				
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'),mainData);
					changeEditor('title1', 'userId', {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:"orgInstPosUserOfficial/findPagerByOrgId",
							cachename:_that.userCacheKey,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					changeEditor('title13', "evaOffId", {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:urlUtil.userPageUrl().url,
							cachename:_that.evaUserCacheKey,
							placeholder:"请输入姓名或岗位"
						}
					},null,mainData)
					if(_that.subEditorObj['title15']){
						changeEditor('title15','objStr1',{
							afterdel:'del1',
							titleTemplate:"<%= '评价项目:' + interviewElementName %>",
							subtitle1Template:"<%= '分值:' + score %>"
						}, null, mainData)
					}
					if(fileId){
						let fielArr = getFormFields(mainData)
						fielArr.push('id')
						for(let index in res[1].value.treportEvaluate){
							if(fielArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.treportEvaluate[index])
								_that.$set(_that.formdata,index,res[1].value.treportEvaluate[index])
							}
						} 
						
						//评价要素
						let scoreResponses = _that._.cloneDeep(res[1].value.scoreResponses)
						_that.subEditorObj["title15"].workList = scoreResponses
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr1", JSON.stringify(scoreResponses))
						_that.$set(_that.formdata, "objStr1", JSON.stringify(scoreResponses))
						delete res[1].value.scoreResponses
						
						if(_that.checkedit){
							changeEditor('title17','resultCode','data',configSelectData(res[2],'code','dicname'),mainData)
						}
							
						_that.$nextTick(function(){
							_that.onSelectUser()
							_that.onSelectZpost()
							_that.$refs.autoform.initSelectIndex('title17','resultCode')
						})
					}
				},options)
			}
		},
		onLoad(options){
			this.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow(){
			let _that = this 
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				
				
				uni.getStorage({
					key:_that.userCacheKey,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userName = d.userName
							formdata.userId = d.userId
							_that.onSelectUser()
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({ 
					key:_that.usersCacheKey,
					success:function(res){
						if(res.data){
							let d = res.data 
							let formdata = _that.$refs.autoform.formdata
							_that.$refs.autoform.changeEditor('title13', 'userIds', {
								initSelect: d
							})
							_that.uIds = d
							uni.removeStorage({
								key:_that.usersCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.evaUserCacheKey,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.evaUserName = d.username
							formdata.evaOffId = d.id
							uni.removeStorage({
								key:_that.evaUserCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
